import { success,error,SQL_ERROR } from './response'
import { analysis } from './request'
import { tokenTo } from './tokenOper'
import redisDB from './redisconf'
 
//app使用
export const dataAnalysis = async (req,res,next) => {
  const { body:data,query:params } = req
  const token = req.headers['src-token']
  const uip = req.headers['u-ip']
  let ipinfo = {}
  if(uip){
    const [ip,id] = uip.split(",")
    ipinfo = {ip,id}
  }
  res.locals = { data,params,token,ipinfo }
  next()
}

//用户验证
export const useVerify = async (req,res,next) => {
  const { token } = res.locals
  if(!token) return res.send(error(500,'未知的用户'))
}

//管理员接口使用
export const adminVerify = async (req,res,next) => {
  const { token } = res.locals
  if(!token) return res.send(error(500,'未知的用户'))
  const { id,type } = await tokenTo(token)
  if(type != 1) return res.send(error(500,'未知的用户'))
  delete res.locals.token
  res.locals.id = id
  console.log("身份验证成功")
  next()
}

//super-admin验证
export const superAdminVerify = async (req,res,next) => {
  // const { token } = res.locals
  // if(req.url !== '/dologin'){
  //   if(!token) return res.send(error(500,'未知的用户'))
  //   const { token:realToken } = redisDB.get(0,'token')
  //   console.log({token,realToken})
  //   if(token != realToken) return res.send(error(403,'token校验失败'))
  //   console.log("身份验证成功")
  // }
  next()
}


//文件接口
export const filedispose = async (req,res,next) => {
  const { file } = req
  res.locals.file = file
  next()
}